package com.yjx.controller;

import com.github.pagehelper.PageInfo;
import com.yjx.mapper.OwnerMapper;
import com.yjx.service.HouseService;
import com.yjx.service.OwnerService;
import com.yjx.util.JsonObject;
import com.yjx.util.R;
import com.yjx.vo.House;
import com.yjx.vo.Owner;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Resource
    private HouseService houseService;

    @RequestMapping("/queryAll")
    public List<House> queryHouseAll(){

       PageInfo<House> pageInfo = houseService.queryHouseAll(1,10,null);
       return pageInfo.getList();

    }

    @RequestMapping("/queryhouseAll")
    public JsonObject<House> queryhouseAll2(String numbers,@RequestParam(defaultValue ="1") Integer page,
                                            @RequestParam(defaultValue ="15") Integer limit){
        House house = new House();
        PageInfo<House> pageInfo=houseService.queryHouseAll(page,limit,numbers);
        JsonObject object = new JsonObject();
        object.setCode(0);
        object.setMsg("Ok");
        object.setCount(pageInfo.getTotal());
        object.setData(pageInfo.getList());
        return  object;

    }
    @RequestMapping("/add")
    public R addHouse(@RequestBody House house){
        boolean flag=houseService.addHouse(house);
        if(flag){
            return R.ok();
        }else {
            return R.fail("添加失败！");
        }
    }

    @RequestMapping("/update")
    public R updateHouse(@RequestBody House house){
        boolean flag=houseService.updateHouse(house);
        if(flag){
            return R.ok();
        }else {
            return R.fail("添加失败！");
        }
    }


    @RequestMapping("deleteByIds")
    public R deleteByIds(Integer[] ids){



        boolean flag =houseService.deleteByIds(ids);

       // boolean flag=houseService.removeByIds(dellist);
        if(flag){
            return R.ok();
        }else {
            return R.fail("删除失败（请确保无户主）");
        }
    }


}
